package tela;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTabbedPane;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JSeparator;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import java.awt.Canvas;
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.table.DefaultTableModel;

import controle.Controlador;

import tabela.TabelaArquivos;
import tabela.TabelaArquivosModel;
import tabela.TabelaArquivosProgressRenderer;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;


public class Tela extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private TabelaArquivos table;
	private TabelaArquivosModel model;
	
	private JLabel lblArquivo;
	
	private File[] sf;
	private JSpinner spinnerNumMaxThreads;
	private JSpinner spinnerMinTamSenha;
	private JSpinner spinnerMaxTamSenha;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Tela frame = new Tela();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Tela() {
		setTitle("BruteForce");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 700, 410);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.setBounds(0, 0, 694, 372);
		contentPane.add(tabbedPane);
		
		JPanel panel = new JPanel();
		tabbedPane.addTab("Status", null, panel, null);
		panel.setLayout(null);
		
		JButton btnAdicionar = new JButton("Adicionar");
		btnAdicionar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				for (File f : sf) 
		            table.addArq(f);
			}
		});
		btnAdicionar.setBounds(546, 36, 109, 25);
		panel.add(btnAdicionar);
		
		JButton btnCarregarArquivo = new JButton("Carregar arquivos");
		btnCarregarArquivo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				JFileChooser chooser = new JFileChooser();
		        chooser.setMultiSelectionEnabled(true);
		        int option = chooser.showOpenDialog(Tela.this);
		        if (option == JFileChooser.APPROVE_OPTION) {
		          sf = chooser.getSelectedFiles();
		          String filelist = "";
			     
		          for (File f : sf) 
		        	  filelist += f.getName() + ", ";
		          
		          filelist = filelist.substring(0, filelist.length() - 2);
		          
		          lblArquivo.setText(filelist);
		        }
		        else {
		        	lblArquivo.setText("");
		        }
			}
		});
		btnCarregarArquivo.setBounds(383, 36, 151, 25);
		panel.add(btnCarregarArquivo);
		
		lblArquivo = new JLabel("");
		lblArquivo.setBounds(22, 42, 349, 14);
		panel.add(lblArquivo);
		
		Canvas canvas = new Canvas();
		canvas.setBackground(new Color(204, 204, 204));
		canvas.setBounds(10, 23, 656, 49);
		panel.add(canvas);
		
		JLabel lblArquivoComSenhas = new JLabel("Arquivo com senhas criptografadas");
		lblArquivoComSenhas.setFont(new Font("Segoe UI", Font.BOLD, 13));
		lblArquivoComSenhas.setBounds(10, 3, 302, 14);
		panel.add(lblArquivoComSenhas);
		model = new TabelaArquivosModel();
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 74, 656, 217);
		panel.add(scrollPane);
		
		table = new TabelaArquivos();
		table.setCellSelectionEnabled(true);
		panel.add(table);
		
		JButton btnStart = new JButton("Start!");
		btnStart.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Controlador c = new Controlador(sf, (Integer) spinnerNumMaxThreads.getValue());
				c.startCracking((Integer) spinnerMinTamSenha.getValue() , (Integer) spinnerMaxTamSenha.getValue());
			}
		});
		btnStart.setBounds(276, 304, 95, 25);
		panel.add(btnStart);
		table.setModel(model);
		table.getColumnModel().getColumn(0).setResizable(false);
		table.getColumnModel().getColumn(0).setPreferredWidth(43);
		table.getColumnModel().getColumn(1).setResizable(false);
		table.getColumnModel().getColumn(1).setPreferredWidth(300);
		table.getColumnModel().getColumn(2).setResizable(false);
		table.getColumnModel().getColumn(2).setPreferredWidth(205);
		table.setBounds(10, 74, 656, 205);
		
		TabelaArquivosProgressRenderer renderer = new TabelaArquivosProgressRenderer();
		table.getColumnModel().getColumn(2).setCellRenderer(renderer);
		
		JPanel panel_1 = new JPanel();
		tabbedPane.addTab("Configura\u00E7\u00F5es", null, panel_1, null);
		panel_1.setLayout(null);
		
		JLabel lblCaracteresParaGerao = new JLabel("Caracteres para gera\u00E7\u00E3o de senhas");
		lblCaracteresParaGerao.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblCaracteresParaGerao.setBounds(10, 11, 570, 14);
		panel_1.add(lblCaracteresParaGerao);
		
		JCheckBox chckbxLetrasMinusculas = new JCheckBox("Letras minusculas");
		chckbxLetrasMinusculas.setSelected(true);
		chckbxLetrasMinusculas.setBounds(6, 32, 174, 23);
		panel_1.add(chckbxLetrasMinusculas);
		
		JCheckBox chckbxLetrasMaiusculas = new JCheckBox("Letras maiusculas");
		chckbxLetrasMaiusculas.setBounds(182, 32, 153, 23);
		panel_1.add(chckbxLetrasMaiusculas);
		
		JCheckBox chckbxNumeros = new JCheckBox("Numeros");
		chckbxNumeros.setSelected(true);
		chckbxNumeros.setBounds(337, 32, 97, 23);
		panel_1.add(chckbxNumeros);
		
		JCheckBox checkBoxEspeciais = new JCheckBox("");
		checkBoxEspeciais.setBounds(436, 32, 26, 23);
		panel_1.add(checkBoxEspeciais);
		
		textField = new JTextField();
		textField.setText("#,?,@,%");
		textField.setBounds(468, 33, 190, 20);
		panel_1.add(textField);
		textField.setColumns(10);
		
		JSeparator separator = new JSeparator();
		separator.setBounds(10, 62, 657, 7);
		panel_1.add(separator);
		
		JLabel lblTamanhoDasSenhas = new JLabel("Tamanho das senhas");
		lblTamanhoDasSenhas.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblTamanhoDasSenhas.setBounds(10, 76, 202, 14);
		panel_1.add(lblTamanhoDasSenhas);
		
		spinnerMinTamSenha = new JSpinner();
		spinnerMinTamSenha.setModel(new SpinnerNumberModel(new Integer(1), new Integer(1), null, new Integer(1)));
		spinnerMinTamSenha.setBounds(244, 74, 29, 20);
		panel_1.add(spinnerMinTamSenha);
		
		spinnerMaxTamSenha = new JSpinner();
		spinnerMaxTamSenha.setModel(new SpinnerNumberModel(new Integer(6), new Integer(1), null, new Integer(1)));
		spinnerMaxTamSenha.setBounds(448, 74, 29, 20);
		panel_1.add(spinnerMaxTamSenha);
		
		JLabel lblMinimo = new JLabel("Minimo: ");
		lblMinimo.setBounds(182, 77, 52, 14);
		panel_1.add(lblMinimo);
		
		JLabel lblMaximo = new JLabel("Maximo: ");
		lblMaximo.setBounds(382, 77, 52, 14);
		panel_1.add(lblMaximo);
		
		JSeparator separator_1 = new JSeparator();
		separator_1.setBounds(10, 103, 657, 7);
		panel_1.add(separator_1);
		
		JLabel lblProcessamento = new JLabel("Processamento");
		lblProcessamento.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblProcessamento.setBounds(10, 129, 132, 14);
		panel_1.add(lblProcessamento);
		
		JLabel lblTempoMaxPor = new JLabel("Tempo max. por senha:");
		lblTempoMaxPor.setBounds(154, 114, 153, 14);
		panel_1.add(lblTempoMaxPor);
		
		textField_1 = new JTextField();
		textField_1.setText("600");
		textField_1.setBounds(309, 111, 72, 20);
		panel_1.add(textField_1);
		textField_1.setColumns(10);
		
		JLabel lblS = new JLabel("s");
		lblS.setBounds(388, 114, 14, 14);
		panel_1.add(lblS);
		
		JLabel lblTempoMaxPor_1 = new JLabel("Tempo max. por arquivo:");
		lblTempoMaxPor_1.setBounds(410, 114, 153, 14);
		panel_1.add(lblTempoMaxPor_1);
		
		textField_2 = new JTextField();
		textField_2.setText("60000");
		textField_2.setColumns(10);
		textField_2.setBounds(565, 111, 72, 20);
		panel_1.add(textField_2);
		
		JLabel label_1 = new JLabel("s");
		label_1.setBounds(644, 114, 14, 14);
		panel_1.add(label_1);
		
		JLabel lblNumMaxDe = new JLabel("Num. max de senhas processadas simultaneamente:");
		lblNumMaxDe.setBounds(154, 141, 329, 14);
		panel_1.add(lblNumMaxDe);
		
		spinnerNumMaxThreads = new JSpinner();
		spinnerNumMaxThreads.setModel(new SpinnerNumberModel(new Integer(3), new Integer(1), null, new Integer(1)));
		spinnerNumMaxThreads.setBounds(495, 139, 68, 20);
		panel_1.add(spinnerNumMaxThreads);
		
		JSeparator separator_2 = new JSeparator();
		separator_2.setBounds(10, 168, 657, 7);
		panel_1.add(separator_2);
		
		JLabel lblFilaDeSenhas = new JLabel("Fila de senhas");
		lblFilaDeSenhas.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblFilaDeSenhas.setBounds(10, 188, 132, 14);
		panel_1.add(lblFilaDeSenhas);
		
		JLabel lblTamanho = new JLabel("Tamanho: ");
		lblTamanho.setBounds(154, 189, 80, 14);
		panel_1.add(lblTamanho);
		
		JSpinner spinner_3 = new JSpinner();
		spinner_3.setModel(new SpinnerNumberModel(new Integer(6000), new Integer(1), null, new Integer(1)));
		spinner_3.setBounds(244, 186, 68, 20);
		panel_1.add(spinner_3);
		
		JLabel lblPercentualParaRealimentao = new JLabel("Percentual para realimenta\u00E7\u00E3o da fila: ");
		lblPercentualParaRealimentao.setBounds(322, 189, 241, 14);
		panel_1.add(lblPercentualParaRealimentao);
		
		JSpinner spinner_4 = new JSpinner();
		spinner_4.setModel(new SpinnerNumberModel(50, 1, 100, 1));
		spinner_4.setBounds(565, 186, 52, 20);
		panel_1.add(spinner_4);
		
		JLabel label = new JLabel("%");
		label.setBounds(627, 189, 14, 14);
		panel_1.add(label);
	}
}
